<?php
declare (strict_types = 1);

namespace app\logic\controller;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Request;

class Excel
{

    /***
     * @param $params array;
     * @return
     */
    public static function Import($params){


        //上传excel文件
        $file= request()->file('goods');

        try {
            $path='/storage/';
            $savename = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
            // 将上传后的文件位置返回给前端
            $savename=str_replace('\\','/',$savename);

        } catch (\think\exception\ValidateException $e) {

            return ['code' => 0, 'msg' =>$e->getMessage()];
        }




        try {
            //获取文件路径
            $filePath = public_path().$path.$savename;
            $spreadsheet = IOFactory::load($filePath);
            $sheetData = $spreadsheet->getActiveSheet()->toArray(true, true, true, true,true);
            $row_num = count($sheetData);
            $import_data = []; //数组形式获取表格数据
            $upload= new Uploads();
            for ($i = 2; $i <= $row_num; $i++) {

                $import_data[$i]['cate_id'] = $sheetData[$i]['A'];
                $import_data[$i]['title'] = $sheetData[$i]['B'];
                $thumb="";
                if($sheetData[$i]['C'] !=1){
                    $thumbs=$upload->UploadExcel($sheetData[$i]['C'],'image');
                    if($thumbs['code']!=200){
                        return json($thumbs);
                    }else{
                        $thumb=$thumbs['data'];
                    }
                }
                $file_address="";
                if($sheetData[$i]['K']!=1){
                    $file_addresss=$upload->UploadExcel($sheetData[$i]['K'],'file');
                    if($file_addresss['code']!=200){
                        return json($file_addresss);
                    }else{
                        $file_address=$file_addresss['data'];
                    }
                }


                $photos=[];
                if($sheetData[$i]['L']!=1) {

                    $fies=$sheetData[$i]['L'];
                    $filename = scandir($fies);
                    // 定义一个数组接收文件名
                    foreach ($filename as $k => $v) {
                        // 跳过两个特殊目录   continue跳出循环
                        if ($v == "." || $v == "..") {
                            continue;
                        }

                        //截取文件名，我只需要文件名不需要后缀;然后存入数组。如果你是需要后缀直接$v即可
                        $path=$fies.'/'.$v;
                        $photo=$upload->UploadExcel($path,'image');
                        if($photo['code']!=200){
                            return json($photo);
                        }else{
                            $photos[]=$photo['data'];
                        }



                    }
                }
                $group_thumb="";
                $group_name="";
                if($params['model_type']==2){
                    if($sheetData[$i]['P']!=1){
                        $group_thumbs=$upload->UploadExcel($sheetData[$i]['P'],'image');
                        if($group_thumbs['code']!=200){
                            return json($group_thumbs);
                        }else{
                            $group_thumb=$group_thumbs['data'];
                        }
                    }
                    $group_name=$sheetData[$i]['O'];
                }


                $import_data[$i]['thumb'] =json_encode($thumb);
                $import_data[$i]['tag'] = $sheetData[$i]['D'];
                $import_data[$i]['status_type']   = $sheetData[$i]['E'];
                $import_data[$i]['file_format']   = $sheetData[$i]['F'];
                $import_data[$i]['selection_criteria']   = $sheetData[$i]['G'];
                $import_data[$i]['goods_sale_type']   = $sheetData[$i]['H'];
                $import_data[$i]['sale_mode']   = $sheetData[$i]['I'];
                $import_data[$i]['file_price']   = $sheetData[$i]['J'];
                $import_data[$i]['file_address']   = !empty($file_address)?json_encode($file_address):"";
                $import_data[$i]['photos']   = json_encode($photos);
                $import_data[$i]['source']   = $sheetData[$i]['M'];
                $import_data[$i]['source_url']   = $sheetData[$i]['N'];
                $import_data[$i]['group_name']   = $group_name;
                $import_data[$i]['group_thumb']   =$group_thumb;
                $import_data[$i]['model_type']   = $params['model_type'];
                $import_data[$i]['user_type']   = 2;
            }

            sort($import_data);

            if (empty($import_data)) {
                return ['code' =>0, 'message' => '数据解析失败'];
            }

            $total_num = count($import_data);
            if ($total_num > 100) {
                return ['code' => 0, 'message' => '数据超出限制，最多100条'];
            }

            return ['code'=>200,'msg'=>'success','data'=>$import_data];

        }catch (\think\exception\ValidateException $e){
            return ['code' => 0, 'msg' =>$e->getMessage()];
        }



     }


}
